package com.senthink.gl.business.domain.service;

import com.senthink.gl.business.auth.AreaAuthority;
import com.senthink.gl.business.auth.DeviceAuthority;
import com.senthink.gl.business.auth.UserRoleEnum;
import com.senthink.gl.business.common.exception.DataAccessException;
import com.senthink.gl.business.domain.entity.*;
import com.senthink.gl.business.domain.repository.UserAreaAuthorityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 用户区域权限DataService
 *
 * @author zooqi@senthink.com
 * @date 2018/03/05
 */
@Service
public class UserAreaAuthorityDataService {

    @Autowired
    private UserAreaAuthorityRepository repository;

    /**
     * 通过用户名查找所有的区域权限(按区域名称排序)
     *
     * @param username 用户名(账户)
     * @return
     */
    public List<UserAreaAuthority> findAllByUsername(String username) {
        try {
            return repository.findAllByUserAccountOrderByAreaName(username);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 保存/更新记录
     *
     * @param authority 需要保存/更新的实体
     * @return
     */
    public UserAreaAuthority save(UserAreaAuthority authority) {
        try {
            return repository.save(authority);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 批量保存记录
     *
     * @param authorities 需要保存的实体
     */
    public List<UserAreaAuthority> saveAll(Iterable<UserAreaAuthority> authorities) {
        try {
            return repository.save(authorities);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 通过uid批量删除
     *
     * @param uid
     * @return
     */
    public long deleteAllByUid(String uid) {
        try {
            return repository.deleteAllByUserUid(uid);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 通过用户名查询用户具有权限的区域列表
     *
     * @param username 用户名
     * @return
     */
    public List<Area> findAreasByUsername(String username) {
        try {
            return repository.findAreasByUsername(username);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 通过用户名查询账号具有权限的区域列表
     *
     * @param account 用户名
     * @return
     */
    public List<Area> findAreasByAccount(String account) {
        try {
            return repository.findAreasByAccount(account);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 查询区域下的用户(姓名)列表
     *
     * @param area
     * @return
     */
    public List<String> findUserNamesByArea(Area area) {
        try {
            return repository.findUserNamesByArea(area);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 查询用户具有除霾器权限的区域数据库ID列表
     *
     * @param uid UID
     * @return
     */
    public List<Long> findAreaIdsWithHazeCleanerAuthorityByUid(String uid) {
        try {
            return repository.findAreaIdsWithHazeCleanerAuthorityByUid(uid, DeviceAuthority.CLEANER);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 查询用户具有除霾器权限的区域列表(按区域名称排序)
     *
     * @param uid UID
     * @return
     */
    public List<Area> findAreasWithHazeCleanerAuthorityByUid(String uid) {
        try {
            return repository.findAreasWithHazeCleanerAuthorityByUid(uid, DeviceAuthority.CLEANER);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 查询用户具有除霾器权限的区域列表
     *
     * @param uid  UID
     * @param aids 区域业务ID列表(筛选条件)
     * @return
     */
    public List<Area> findAreasWithHazeCleanerAuthorityByUid(String uid, Iterable<String> aids) {
        try {
            if (!aids.iterator().hasNext()) {
                return new LinkedList<>();
            }
            return repository.findAreasWithHazeCleanerAuthorityByUid(uid, DeviceAuthority.CLEANER, aids);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 查询用户具有某设备权限的区域数据库ID列表
     *
     * @param uid             用户UID
     * @param deviceAuthority 设备权限字符串, 参见{@link DeviceAuthority}
     * @param areaId          区域数据库ID(筛选条件)
     * @return
     */
    public List<Long> findAreaIdsWithDeviceAuthorityByUid(String uid, String deviceAuthority, Long areaId) {
        try {
            if (areaId == null) {
                return repository.findAreaIdsWithDeviceAuthorityByUid(uid, deviceAuthority);
            }
            return repository.findAreaIdsWithDeviceAuthorityByUid(uid, deviceAuthority, areaId);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    public List<String> findAreaAidsWithDeviceAuthorityByUid(String uid, String deviceAuthority, Long areaId) {
        try {
            if (areaId == null) {
                return repository.findAreaAidsWithDeviceAuthorityByUid(uid, deviceAuthority);
            }
            return repository.findAreaAidsWithDeviceAuthorityByUid(uid, deviceAuthority, areaId);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 查询用户具有某设备权限的区域列表(按区域名称排序)
     *
     * @param uid             用户UID
     * @param deviceAuthority 设备权限字符串, 参见{@link DeviceAuthority}
     * @return
     */
    public List<Area> findAreasWithDeviceAuthorityByUid(String uid, String deviceAuthority) {
        try {
            return repository.findAreasWithDeviceAuthorityByUid(uid, deviceAuthority);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 查询用户是否有某设备的权限
     *
     * @param uid
     * @param deviceAuthority
     * @return
     */
    public boolean findHasDeviceAuthorityByUid(String uid, String deviceAuthority) {
        try {
            return repository.countAreasWithDeviceAuthorityByUid(uid, deviceAuthority) > 0;
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 查询用户具有权限的网关数据库ID列表
     *
     * @param account 账户
     * @return
     */
    public List<Long> findAreaIdsByAccount(String account) {
        try {
            return repository.findAreaIdsByAccount(account);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 查询用户具有权限的网关数据库ID列表
     *
     * @param account 账户
     * @return
     */
    public List<String> findAreaAidsByAccount(String account) {
        try {
            return repository.findAreaAidsByAccount(account);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }









    /**
     * 根据用户账户名以及区域aid获取用户该区域下的设备权限
     *
     * @param aid
     * @param account
     * @return
     */
    public Optional<UserAreaAuthority> findTopByAreaAidAndUserAccount(String aid, String account) {
        try {
            return repository.findTopByAreaAidAndUserAccount(aid, account);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 查询用户所有区域设备权限(按区域名称排序)
     *
     * @param uid 用户UID
     * @return
     */
    public List<AreaAuthority> findUserAreaAuthorities(String uid) {
        try {
            return repository.findUserAreaAuthorities(uid);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 查询用户区域数据库ID与设备权限的对应关系
     *
     * @param uid
     * @return 区域数据库ID==>设备设备权限
     */
    public Map<Long, String> findUserAreaIdAuthorityMap(String uid) {
        try {
            List<Object[]> list = repository.findUserAreaIdAuthorityMap(uid);

            Map<Long, String> map = new HashMap<>(list.size());
            for (Object[] objects : list) {
                map.put((Long) objects[0], (String) objects[1]);
            }
            return map;
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 查询具有区域权限的所有用户的手机号码
     *
     * @param areas 区域列表
     * @return
     */
    public List<String> findUserPhonesByAreas(Iterable<Area> areas) {
        try {
            if (!areas.iterator().hasNext()) {
                return new LinkedList<>();
            }
            return repository.findUserPhonesByAreas(areas, UserRoleEnum.ROLE_USER.name(), true);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }
}
